﻿using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.DTO;
using Pay.TO;
using Pay.Core;
using System.Threading.Tasks;
using Pay.Token.Utils;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 集市订单
    /// </summary>
    [AppAuthorize("Member")]
    public class BazaarOrderController : GeneralController {
        private ITokenBazaarOrderService _iTokenBazaarOrderService;
        private ITokenBazaarProductService _iTokenBazaarProductService;
        private IMerchantService _iMerchantService;
        private readonly IShippingAddressService _iShippingAddressService;
        private ILogisticsDetailsService _iLogisticsDetailsService;
        public BazaarOrderController(ITokenBazaarOrderService iTokenBazaarOrderService, ITokenBazaarProductService iTokenBazaarProductService,
            IShippingAddressService iShippingAddressService, IMerchantService iMerchantService, ILogisticsDetailsService iLogisticsDetailsService) {
            _iTokenBazaarOrderService = iTokenBazaarOrderService;
            _iTokenBazaarProductService = iTokenBazaarProductService;
            _iShippingAddressService = iShippingAddressService;
            _iMerchantService = iMerchantService;
            _iLogisticsDetailsService = iLogisticsDetailsService;
        }
        /// <summary>
        /// 我的
        /// </summary>
        /// <returns></returns>
        public ActionResult Index() {
            token_user_info member = new token_user_info();
            if (UserId > 0) {
                member = _iMerchantService.GetTokenUser(UserId);
            }
            ViewBag.member = member;
            ViewBag.CurUserId = UserId;
            return View();
        }
        /// <summary>
        /// 确认订单
        /// </summary>
        /// <returns></returns>
        public ActionResult ConfirmOrder(long id, string wxOpenId) {
            var model = _iTokenBazaarProductService.GetProduct(id);
            var address = _iShippingAddressService.GetByDefault(UserId);
            ViewBag.address = address;
            ViewBag.img_url = RequestMethod.token_imgurl;
            ViewBag.wxOpenId = wxOpenId;
            return View(model);
        }
        /// <summary>
        /// 确认订单 红钻
        /// </summary>
        /// <param name="id"></param>
        /// <param name="addressId"></param>
        /// <returns></returns>
        public JsonResult Confirm(long id, long addressId) {
            bool result = _iTokenBazaarOrderService.CreateOrder(id, UserId, addressId);
            return Json(ResultToJson.Json(result ? 1000 : 1001, result ? "交易成功" : "交易失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 确认订单 现金支付
        /// </summary>
        /// <param name="id"></param>
        /// <param name="addressId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> Pay(long id, long addressId, string wxOpenId) {
            var api_result = new ApiResultMessage { result_msg = "操作成功" };
            string ip = GetHostAddress();
            var model = _iTokenBazaarOrderService.ValidateOrder(id, UserId, addressId,wxOpenId, ip);
            var result = await HChannelApi.Current.PostApi<mall_h5_result>(model);
            if (result == null) {
                throw new ServerErrorException("网络异常");
            }
            if (result.result_code == 1000) {
                if (model.is_wx_pub_account_pay) {//公众号支付
                    api_result.result_msg = result.pay_info;
                } else {
                    api_result.result_msg = result.code_url;
                }
                api_result.result_code = 1000;
            } else {
                throw new ServerErrorException(result.result_msg);
            }
            return Json(api_result, JsonRequestBehavior.AllowGet);
        }
      
            /// <summary>
            ///  （卖家）订单
            /// </summary>
            /// <returns></returns>
            public ActionResult SellOrder() {
            return View();
        }
        public PartialViewResult _SellOrder(BazaarOrderQuery query) {
            query.is_del = false;
            query.merchant_id = UserId;
            query.merchant_type = 2;
            var list = _iTokenBazaarOrderService.Gets(query);
            ViewBag.img_url = RequestMethod.token_imgurl;
            return PartialView(list);
        }
        /// <summary>
        /// (买家) 订单
        /// </summary>
        /// <returns></returns>
        public ActionResult BuyOrder() {
            return View();
        }
        public PartialViewResult _BuyOrder(BazaarOrderQuery query) {
            query.is_del = false;
            query.merchant_id = UserId;
            query.merchant_type = 1;
            var list = _iTokenBazaarOrderService.Gets(query);
            ViewBag.img_url = RequestMethod.token_imgurl;
            return PartialView(list);
        }
        /// <summary>
        ///订单详情
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderDetail(long id) {
            var model = _iTokenBazaarOrderService.Get(id);
            ViewBag.img_url = RequestMethod.token_imgurl;
            if (model.seller_id == UserId) return View("SellOrderDetail", model);
            else return View(model);
        }
        /// <summary>
        /// 订单发货
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult OrderShip(long id) {
            ViewBag.order_id = id;
            List<DropdownItem> expressList = RequestMethod.express.ToObject<List<DropdownItem>>();
            ViewBag.ExpressList = expressList;
            return View();
        }
        /// <summary>
        /// 卖家发货
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JsonResult Ship(TokenBazaarOrderShipModel model) {
            model.merchant_id = UserId;
            bool result = _iTokenBazaarOrderService.Deliver(model);
            return Json(ResultToJson.Json(result ? 1000 : 1001, result ? " 发货成功" : "发货失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 卖家删除订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult SellDelete(long id) {
            bool result = _iTokenBazaarOrderService.Delete(id, UserId, 2);
            return Json(ResultToJson.Json(result ? 1000 : 1001, result ? " 删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 买家删除订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult BuyDelete(long id) {
            bool result = _iTokenBazaarOrderService.Delete(id, UserId, 1);
            return Json(ResultToJson.Json(result ? 1000 : 1001, result ? " 删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 卖家取消订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Cancle(long id) {
            bool result = _iTokenBazaarOrderService.Cancle(id, UserId);
            return Json(ResultToJson.Json(result ? 1000 : 1001, result ? "取消成功" : "取消失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 卖家发货
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JsonResult Deliver(TokenBazaarOrderShipModel model) {
            model.merchant_id = UserId;
            bool result = _iTokenBazaarOrderService.Deliver(model);
            return Json(ResultToJson.Json(result ? 1000 : 1001, result ? "发货成功" : "发货失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 买家确认收货
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Receive(long id) {
            bool result = _iTokenBazaarOrderService.Receive(id, UserId);
            return Json(ResultToJson.Json(result ? 1000 : 1001, result ? "收货成功" : "收货失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 物流信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult OrderLogistics(long id) {
            var model = _iTokenBazaarOrderService.Get(id);
            order_dto dto = new order_dto();
            dto.shipping = new shipping_dto();
            dto.shipping.express_no = model.express_no;
            dto.shipping.express_company_name = model.express_company_name;
            dto.shipping.ship_order_number = model.ship_order_number;
            var data = _iLogisticsDetailsService.GetList(dto);
            var result = new order_logistic_results();
            result.order_id = model.id;
            result.ship_order_number = model.ship_order_number;
            result.express_company_name = model.express_company_name;
            result.logistic_results = data;
            return View(result);
        }
    }
}